/**
 * 
 */
package info.niwota.start;

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SimpleCursorTreeAdapter;
import android.widget.TextView;

/**
 * @author qiangli
 * 
 */
public abstract class AppExpandableListAdapter extends SimpleCursorTreeAdapter {

	private static class ViewHolder {
		TextView line1;
		ImageView icon;
	}

	public AppExpandableListAdapter(Context ctx, int entries) {
		super(ctx, new ArrayCursor(ctx.getResources().getStringArray(entries)), R.layout.list_item_group, new String[] {},
				new int[] {}, R.layout.list_item_child, new String[] {},
				new int[] {});
	}

	@Override
	protected ArrayCursor getChildrenCursor(Cursor groupCursor) {
		int id = groupCursor.getInt(0);
		
		List<? extends Object> list = load(id);
		if (list == null) {
			return null;
		}
		return new ArrayCursor(list.toArray());
	}

	@Override
	public ArrayCursor getChild(int groupPosition, int childPosition) {
		return (ArrayCursor) super.getChild(groupPosition, childPosition);
	}

	@Override
	public ArrayCursor getGroup(int groupPosition) {
		return (ArrayCursor) super.getGroup(groupPosition);
	}

	protected abstract List<? extends Object> load(int group);

	@Override
	public View newGroupView(Context context, Cursor cursor,
			boolean isExpanded, ViewGroup parent) {
		View v = super.newGroupView(context, cursor, isExpanded, parent);
		ImageView iv = (ImageView) v.findViewById(R.id.icon);
		ViewGroup.LayoutParams p = iv.getLayoutParams();
		p.width = ViewGroup.LayoutParams.WRAP_CONTENT;
		p.height = ViewGroup.LayoutParams.WRAP_CONTENT;
		ViewHolder vh = new ViewHolder();
		vh.line1 = (TextView) v.findViewById(R.id.line1);
		vh.icon = (ImageView) v.findViewById(R.id.icon);
		vh.icon.setPadding(0, 0, 1, 0);
		v.setTag(vh);
		return v;
	}

	@Override
	public View newChildView(Context context, Cursor cursor,
			boolean isLastChild, ViewGroup parent) {
		View v = super.newChildView(context, cursor, isLastChild, parent);
		ViewHolder vh = new ViewHolder();
		vh.line1 = (TextView) v.findViewById(R.id.line1);
		vh.icon = (ImageView) v.findViewById(R.id.icon);
		vh.icon.setBackgroundDrawable(null);
		vh.icon.setPadding(0, 0, 1, 0);
		v.setTag(vh);
		return v;
	}

	@Override
	public void bindGroupView(View view, Context context, Cursor cursor,
			boolean isexpanded) {
		ViewHolder vh = (ViewHolder) view.getTag();
		ArrayCursor c = (ArrayCursor) cursor;
		String t = (String) c.getObject();
		vh.line1.setText(t);
	}

	@Override
	public void bindChildView(View view, Context context, Cursor cursor,
			boolean islast) {
		ViewHolder vh = (ViewHolder) view.getTag();
		ArrayCursor c = (ArrayCursor) cursor;
		ResourceInfo ri = (ResourceInfo) c.getObject();
		if (ri != null) {
			vh.line1.setText(ri.getTitle());
			ImageView iv = vh.icon;
			Drawable d = ImageUtil.filter(ri.getIcon()); //48x48
			iv.setImageDrawable(d);
		}
	}
}
